﻿package
{
	import org.flixel.*;
	//import org.flixel.plugin.photonstorm.FlxWeapon;
	//import org.flixel.plugin.photonstorm.FlxCollision;
	//import flashx.textLayout.formats.Float;
	
	
	public class masa extends FlxSprite
	{
		[Embed(source="data/masa.png")] private var ImgMasa:Class;
		//[Embed(source="data/golpe.mp3")] private var SoundHurt:Class;

		internal var charge: int;
		internal var allCharges: FlxGroup;
		
		public function masa(X:Number, Y:Number, initialCharge:int, allChargesVar:FlxGroup)
		{
			super(X, Y);
			loadGraphic(ImgMasa,true,true);
			maxVelocity.x = 100;			//walking speed
			maxVelocity.y = 100;
			//acceleration.y = 400;			    //gravity
			drag.x = maxVelocity.x*4;		//deceleration (sliding to a stop)
			drag.y = maxVelocity.y*4;
			
			//tweak the bounding box for better feel
			width = 25;
			height = 25;
			offset.x = 0;
			offset.y = 0;
			
			charge = initialCharge;
			solid = true;
			moves = false;
			immovable = true;
			//immovable // no es afectado por las colisiones // tipo Boolean
			//mass y elasticity //masa y rebote //tipo Number
			//maxVelocity // tipo FlxPoint;

			allCharges = allChargesVar;
			
			addAnimation("positive",[0],0,false);
			addAnimation("negative",[1],0,false);
			addAnimation("neutral",[2],0,false);
			
			(FlxG.state as Level1).add(this);			
		}
		
		
		public function getCharge():int
		{
			return charge;
		}
				
		public function setCharge(newCharge:int):void
		{
			charge = newCharge;
		}		
		
		private function calculateSquaredDistance(point1:FlxPoint, point2:FlxPoint):Number
		{
			var SD:Number;
			if( (((point1.x - point2.x) ^ 2) == 0) || (((point1.y - point2.y) ^ 2) == 0) )
			 SD = 1;
			else SD = ((point1.x - point2.x) ^ 2) + ((point1.y - point2.y) ^ 2);
			return SD;
		}
		
		
		private function calculateAcceleration():FlxPoint
		{
			var i:uint = 0;
			var membCharge:int;
			var membPosition:FlxPoint = new FlxPoint(0,0);
			var membAcceleration:FlxPoint = new FlxPoint(0,0);
			var accelerationTotal:FlxPoint = new FlxPoint(0,0);
			var sign:int = 1;
			var k:Number = 5;
			
			var members:Array = allCharges.members;
			while((i < members.length)&&((members[i] as masa)!=this))
				{
				//membPosition = (members[i] as masa).getScreenXY();
				//membPosition.y = (members[i] as masa).y;
				//if((members[i] as masa)==this)trace("rechangos");
				membPosition = (members[i] as masa).getMidpoint();
				membCharge = (members[i++] as masa).getCharge();
				membAcceleration.x = k * (this.charge * membCharge) / calculateSquaredDistance(this.getMidpoint(),membPosition);
				
				if(this.getMidpoint().x < membPosition.x)
					{
					if((this.charge * membCharge) < 0)
						 sign = 1;
					else sign = -1;
					}
				else if(this.getMidpoint().x > membPosition.x)
					{
					if((this.charge * membCharge) < 0)
						 sign = -1;
					else sign = 1;
					}
				else     sign = 0;	
				accelerationTotal.x += sign * Math.abs(membAcceleration.x);		
						
				membAcceleration.y = k * (this.charge * membCharge) / calculateSquaredDistance(this.getMidpoint(),membPosition);
				if(this.getMidpoint().y < membPosition.y)
					{
					if((this.charge * membCharge) < 0)
						 sign = 1;
					else sign = -1;
					}
				else if(this.getMidpoint().y > membPosition.y)
					{
					if((this.charge * membCharge) < 0)
						 sign = -1;
					else sign = 1;
					}
				else 	 sign = 0;	
				accelerationTotal.y += sign * Math.abs(membAcceleration.y);	
				}
			return accelerationTotal;
		}
		
		override public function update():void
		{
				if(charge > 0)
				play("positive");
			else if(charge < 0)	
				play("negative");
			else 
			    play("neutral");

			acceleration.x = calculateAcceleration().x;	
			acceleration.y = calculateAcceleration().y;	
			
		}
		
		
	}
}

